package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.ImageDTO;
import com.fuli.app.service.ImageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(tags = "二维码图片")
@RestController
@RequestMapping("/image")
public class ImageController {
    @Autowired
    private ImageService imageService;
    public static final long MAX_SIZE = 1024 * 512;
    public static final Set<String> IMAGE_SUFFIX_SET = new HashSet<>(Arrays.asList(
            "jpg", "JPG", "jpge", "JPGE", "png", "PNG", "gif", "GIF"));

    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    public ActionResult<ImageDTO> upload(@ApiParam("图片") @RequestParam("image") MultipartFile file,
                                         @ApiParam("描述") @RequestParam String description,
                                         @ApiParam("班级id") @RequestParam String classId) {

        if (file.getSize() > MAX_SIZE)
            return new ActionResult<>(0, "图片大小超过限制");
        if (file.isEmpty())
            return new ActionResult<>(0, "文件不存在");
        int lastIndex = file.getOriginalFilename().lastIndexOf('.');
        if (lastIndex == -1)
            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
        String suffix = file.getOriginalFilename().substring(lastIndex + 1);
        if (StringUtils.isEmpty(suffix))
            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
        if (!IMAGE_SUFFIX_SET.contains(suffix))
            return new ActionResult<>(0, "文件格式不支持，仅支持jpg,jpge,png,gif格式");
        if (StringUtils.isEmpty(classId))
            return new ActionResult<>(0, "班级id不能为空");
        return imageService.upload(file, description, classId);

    }

    @ApiOperation(value = "删除图片")
    @DeleteMapping("/delete/{id}")
    public ActionResult<String> delete(@ApiParam("id") @PathVariable String id) {
        return imageService.delete(id);
    }

    @ApiOperation(value = "获取图片")
    @GetMapping("/get/{id}")
    public ActionResult<ImageDTO> get(@ApiParam("id") @PathVariable String id) {
        return imageService.get(id);
    }

    @ApiOperation(value = "通过班级id获取图片")
    @GetMapping("/getByClassId/{classId}")
    public ActionResult<List<ImageDTO>> getByClassId(@ApiParam("id") @PathVariable String id) { return imageService.getByClassId(id); }

    @ApiOperation(value = "分页查询图片")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<ImageDTO>> pageQuery(@ApiParam("班级id") @RequestParam(required = false) String classId,
                                                        @ApiParam("页码") @RequestParam(required = false) Integer pageNo,
                                                        @ApiParam("数量") @RequestParam(required = false) Integer pageSize) {
        if (pageNo == null || pageNo <= 0)
            pageNo = 1;
        if (pageSize == null || pageSize <= 0)
            pageSize = 10;
        return imageService.pageQuery(classId, pageNo, pageSize);
    }

}
